package com.it.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages = "com.it",
        excludeFilters = {
                //基于直接排除指定直接下的bean资源
                @ComponentScan.Filter(
                        type = FilterType.ANNOTATION,
                        classes = {Controller.class, ControllerAdvice.class, RestController.class, RestControllerAdvice.class}
                )
        })
@EnableTransactionManagement//开启事务注解支持
@Import(MybatisConfig.class)//功能上等价于<import resource="classpath:applicationContext-dao.xml.bak"/>
public class SpringConfig {
    /**
     * 如果没有给bean显示取名称,默认就是方法名称,所以此时方法名称必须是transactionManager
     * @param dataSource
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager(@Autowired DataSource dataSource){
        DataSourceTransactionManager dstm = new DataSourceTransactionManager();
        dstm.setDataSource(dataSource);
        return dstm;
    }
}